package proxy.wrappers;

import android.os.IBinder;
import android.os.IInterface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/* loaded from: classes2.dex */
public final class ServiceManager {
    public static final String PACKAGE_NAME = "com.android.shell";
    public static final int USER_ID = 0;
    private ActivityManager activityManager;
    private ClipboardManager clipboardManager;
    private DisplayManager displayManager;
    private final Method getServiceMethod;
    private InputManager inputManager;
    private PackageManager packageManager;
    private PowerManager powerManager;
    private StatusBarManager statusBarManager;
    private WindowManager windowManager;

    public ServiceManager() {
        try {
            this.getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

    private IInterface getService(String service, String type) {
        try {
            IBinder binder = (IBinder) this.getServiceMethod.invoke(null, service);
            Method asInterfaceMethod = Class.forName(type + "$Stub").getMethod("asInterface", IBinder.class);
            return (IInterface) asInterfaceMethod.invoke(null, binder);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

    public WindowManager getWindowManager() {
        if (this.windowManager == null) {
            this.windowManager = new WindowManager(getService("window", "android.view.IWindowManager"));
        }
        return this.windowManager;
    }

    public DisplayManager getDisplayManager() {
        if (this.displayManager == null) {
            this.displayManager = new DisplayManager(getService("display", "android.hardware.display.IDisplayManager"));
        }
        return this.displayManager;
    }

    public InputManager getInputManager() {
        if (this.inputManager == null) {
            try {
                Method getInstanceMethod = android.hardware.input.InputManager.class.getDeclaredMethod("getInstance", new Class[0]);
                android.hardware.input.InputManager im = (android.hardware.input.InputManager) getInstanceMethod.invoke(null, new Object[0]);
                this.inputManager = new InputManager(im);
            } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                throw new AssertionError(e);
            }
        }
        return this.inputManager;
    }

    public PowerManager getPowerManager() {
        if (this.powerManager == null) {
            this.powerManager = new PowerManager(getService("power", "android.os.IPowerManager"));
        }
        return this.powerManager;
    }

    public StatusBarManager getStatusBarManager() {
        if (this.statusBarManager == null) {
            this.statusBarManager = new StatusBarManager(getService("statusbar", "com.android.internal.statusbar.IStatusBarService"));
        }
        return this.statusBarManager;
    }

    public ClipboardManager getClipboardManager() {
        if (this.clipboardManager == null) {
            IInterface clipboard = getService("clipboard", "android.content.IClipboard");
            if (clipboard == null) {
                return null;
            }
            this.clipboardManager = new ClipboardManager(clipboard);
        }
        return this.clipboardManager;
    }

    public ActivityManager getActivityManager() {
        if (this.activityManager == null) {
            try {
                Class<?> cls = Class.forName("android.app.ActivityManagerNative");
                Method getDefaultMethod = cls.getDeclaredMethod("getDefault", new Class[0]);
                IInterface am = (IInterface) getDefaultMethod.invoke(null, new Object[0]);
                this.activityManager = new ActivityManager(am);
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
        return this.activityManager;
    }

    public PackageManager getPackageManager() {
        if (this.packageManager == null) {
            this.packageManager = new PackageManager(getService("package", "android.content.pm.IPackageManager"));
        }
        return this.packageManager;
    }

    public WindowManager newWindowManager() {
        return new WindowManager(getService("window", "android.view.IWindowManager"));
    }
}